﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;

namespace Reborn_Server.App1AppHandler
{
    public class CreatureSpawn : Creature
    {
        private Timer Timer_Lifespan = new Timer();
        public void StartLifespanTimer(int seconds)
        {
            Timer_Lifespan.Interval = seconds * 1000;
            Timer_Lifespan.AutoReset = false;
            Timer_Lifespan.Elapsed += new ElapsedEventHandler(Timer_Lifespan_Elapsed);
            Timer_Lifespan.Start();
        }

        public CreatureSpawn(AppHandler2 gameHandler) : base(gameHandler) { }

        public override int HP
        {
            set
            {
                if (value < 1)
                    Timer_Lifespan_Elapsed(null, null);

                base.HP = value;
            }
        }

        //Flag indicates this creatures loot came from an existing corpse (Raise Corpses spell). The loot has already been processed accordingly
        //such as binding have been set and coins have been added.
        public bool isCorpseLoot = false;

        private bool lifespanExpired = false;

        public override void DropLoot(int SlayingCharID)
        {
            //Loot already processed, just drop it.
            if (isCorpseLoot == true)
            {
                if (LootItems.Count > 0)
                {
                    GameHandler.GetMap(MapID, Instance).LootMngr.DropItems(MapX, MapY, LootItems, false);
                    LootItems = new List<Loot>();
                }
                return;
            }

            if (lifespanExpired == false)
                base.DropLoot(SlayingCharID);
        }

        private void Timer_Lifespan_Elapsed(object sender, ElapsedEventArgs e)
        {
            LockItem lockItem = GameHandler.GetLockItem(MapID, Instance);
            lock (lockItem.LockObj)
            {
                if (base.HP < 1)
                    return;

                base.HP = 0;

                Timer_Lifespan.Stop();
                Timer_Lifespan.Dispose();

                currentMapItem.parentMap.removeCreatureToProcess(this);

                Timer_Regen.Stop();
                creatureAI.Timer_CreatureRound.Stop();
                creatureAI.Timer_CreatureRespawn.Stop();

                //Only remove the creature if the timer caused this. Otherwise, something else killed it and will have handled this.
                if (sender != null)
                {
                    //Normal loot will not be dropped.
                    lifespanExpired = true;

                    Map CurrentMap = GameHandler.GetMap(MapID, Instance);
                    MapItem CurrentMapItem = null;
                    List<Character> CharactersWithin8 = new List<Character>();
                    foreach (MapItem mapitem in CurrentMap.MapItems)
                    {
                        if (Math.Abs(mapitem.MapX - MapX) <= 8 && Math.Abs(mapitem.MapY - MapY) <= 8)
                        {
                            foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                            {
                                if (creaturebase is Character)
                                    CharactersWithin8.Add((Character)creaturebase);
                            }
                        }
                        if (mapitem.MapX == MapX && mapitem.MapY == MapY)
                        {
                            mapitem.CreaturesBases.Remove(this);
                            CurrentMapItem = mapitem;
                        }
                    }

                    //Call this anyway under the circumstance of carrying loot from a corpse.
                    this.DropLoot(-1);

                    foreach (Character character in CharactersWithin8)
                    {
                        character.AddMapItem(CurrentMapItem);
                        character.PlaySound(this.SoundDeath, this.MapX, this.MapY);
                    }
                }

            }//End Lock

        }
    }
}
